package com.dyh.quickdemo.demos.thre;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
@Author=Joey Wheeler
@Date=2020/10/26 11:36
*/
@Slf4j
public class ThreLocalUp {
    ThreadLocal<String> threadLocal = new ThreadLocal(){
//        @Override
//        protected String initialValue() {
//            return "";
//        }
    };
    ExecutorService es = Executors.newFixedThreadPool(3);

    public void work(){

        Thread that = Thread.currentThread();
        for (int i=0;i<3;i++){
            es.submit(()->{
                //只能拿到自己的
                log.info(Thread.currentThread().getName()+"-"+threadLocal.get());
            });
        }

    }

    /*
    11:51:03.080 [main] INFO com.dyh.quickdemo.demos.thre.ThreLocalUp - main114514
11:51:03.084 [pool-2-thread-3] INFO com.dyh.quickdemo.demos.thre.ThreLocalUp - pool-2-thread-3-null
11:51:03.085 [pool-2-thread-2] INFO com.dyh.quickdemo.demos.thre.ThreLocalUp - pool-2-thread-2-null
11:51:03.084 [pool-2-thread-1] INFO com.dyh.quickdemo.demos.thre.ThreLocalUp - pool-2-thread-1-null
    */

    @Test
    public void test(){
        ThreLocalUp threLocalUp =  new ThreLocalUp();
        threLocalUp.threadLocal.set("114514");

        log.info( Thread.currentThread().getName()+threLocalUp.threadLocal.get());

        threLocalUp.work();
    }

}
